package com.eurekaconsumerfeign.controller;

import com.eurekaconsumerfeign.client.DcClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author: create by Young
 * @version: v1.0
 * @description: 创建一个接口用来消费eureka-client提供的接口
 * @date:2019/3/31 17:22
 */
@RestController
public class DcController {

/*
	@Autowired
	LoadBalancerClient loadBalancerClient;

	@Autowired
	RestTemplate restTemplate;
*/

	@Autowired
	DcClient dcClient;

	@GetMapping("/consumer")
	public String dc() {
		//负载均衡的选出一个eureka-client的服务实例
		/*ServiceInstance serviceInstance = loadBalancerClient.choose("eureka-client");
		String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort();
		System.out.println(url);
		return restTemplate.getForObject(url,String.class);*/
		// Ribbon拦截器自动的去选取服务实例
		//return restTemplate.getForObject("http://eureka-client/dc",String.class);

		//通过定义的Feign客户端来访问服务提供方的接口
		return dcClient.consumer();
	}
}

